﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Principal;
using System.Security.Cryptography;
using System.IO;
using Haozes.FxClient.CommUtil;

namespace Haozes.FxClient.Security
{
    public static class SecurityHelper
    {
        // Fields
        private static string _secToken = WindowsIdentity.GetCurrent().User.Value;
        public static readonly byte[] RgbIV = new byte[] { 0x1f, 0x4e, 0x81, 12, 0xa8, 0x20, 0xfe, 0x42 };

        // Methods
        public static string DecryptPassword(string text)
        {
            if (text.Length <= 0)
            {
                return string.Empty;
            }
            return InnerDecrypt(Convert.FromBase64String(text), _secToken);
        }

        public static string EncryptPass(string sid, string domain, string oldPass, string newPass)
        {
            string s = sid + ":" + domain + ":" + oldPass;
            byte[] bytes = Encoding.UTF8.GetBytes(s);
            byte[] buffer2 = new MD5CryptoServiceProvider().ComputeHash(bytes);
            string str2 = StringHelper.Hex2Str(buffer2) + ":" + newPass;
            TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
            provider.Key = buffer2;
            MemoryStream stream = new MemoryStream();
            using (CryptoStream stream2 = new CryptoStream(stream, new TripleDESCryptoServiceProvider().CreateEncryptor(buffer2, RgbIV), CryptoStreamMode.Write))
            {
                byte[] buffer = Encoding.UTF8.GetBytes(str2);
                stream2.Write(buffer, 0, buffer.Length);
                stream2.FlushFinalBlock();
                stream2.Close();
            }
            byte[] buffer4 = stream.ToArray();
            stream.Close();
            return StringHelper.Hex2Str(buffer4);
        }

        public static string EncryptPassword(string plain)
        {
            if (plain.Length <= 0)
            {
                return string.Empty;
            }
            return Convert.ToBase64String(InnerEncrypt(plain, _secToken));
        }

        public static byte[] GetSha1(byte[] a)
        {
            using (SHA1 sha = SHA1.Create())
            {
                return sha.ComputeHash(a);
            }
        }

        private static string InnerDecrypt(byte[] buffer, string keyToken)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(keyToken);
            byte[] rgbKey = new MD5CryptoServiceProvider().ComputeHash(bytes);
            MemoryStream stream = new MemoryStream(buffer, false);
            using (CryptoStream stream2 = new CryptoStream(stream, new TripleDESCryptoServiceProvider().CreateDecryptor(rgbKey, RgbIV), CryptoStreamMode.Read))
            {
                StreamReader reader = new StreamReader(stream2);
                return reader.ReadToEnd();
            }
        }

        private static byte[] InnerEncrypt(string text, string keyToken)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(keyToken);
            byte[] rgbKey = new MD5CryptoServiceProvider().ComputeHash(bytes);
            MemoryStream stream = new MemoryStream();
            using (CryptoStream stream2 = new CryptoStream(stream, new TripleDESCryptoServiceProvider().CreateEncryptor(rgbKey, RgbIV), CryptoStreamMode.Write))
            {
                byte[] buffer = Encoding.UTF8.GetBytes(text);
                stream2.Write(buffer, 0, buffer.Length);
                stream2.FlushFinalBlock();
                stream2.Close();
            }
            return stream.ToArray();
        }
    }
}
